<?php

namespace App\Http\Controllers;

use App\Models\Disclosure;
use App\Services\DisclosureService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class DisclosureController extends Controller
{
    protected $DisclosureService;
    public function __construct(DisclosureService $DisclosureService)
    {
        $this->DisclosureService = $DisclosureService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $type_id = $Request->type_id;
        $sql = $this->DisclosureService->get($search);
        $sql->when($type_id, function ($query, $type_id) {
            return $query->where(function($query) use ($type_id) {
                $query->where('type_id',$type_id);
            });
        });
        $sql->where('project_id',$Request->project_id);
        $data = $sql->orderBy('created_at','desc')->get();

        $type = $this->DisclosureService->getType('')->get();
        return response()->json(['code' => '200', 'msg' => '操作成功','type'=>$type,'data'=>$data]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Disclosure::with('users')->where('id',$Request->id)->first();
        //如果是绑定的用户记录是否看过
        foreach ($data->users as $key => $value) {
            if($value->user_id == Auth::user()->id){
                $value->looks = 1;
                $value->save();
            }
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }
}
